package sns.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sns.bean.Tag;
import sns.bean.User;

public class TagDao extends HibernateDaoSupport{

	public void save(Tag tag){
		getSession().save(tag);
	}
	
	public void merge(Tag tag){
		getSession().merge(tag);
	}

	public void delete(Tag tag){
		getSession().delete(tag);
	}
	
	/**
	 * get tag by its id
	 * 
	 * @param idtag
	 * @return
	 */
	public Tag getTagById(Long idtag){
		if(idtag==null){
			return null;
		}
		Query q = getSession().createQuery("select tag from Tag as tag where tag.idtag = :idtag");
		q.setLong("idtag", idtag);
		return (Tag)q.uniqueResult();
	}
	
	/**
	 * get tags of the user
	 * 
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Tag> getTagsOfUser(User user){
		if(user==null){
			return null;
		}
		Query q = getSession().createQuery("select tag from Tag as tag where tag.tagUser.iduser = :iduser");
		q.setLong("iduser", user.getIduser());
		return (List<Tag>)q.list();
	}
}
